package com.livery.app.model;

import java.util.ArrayList;
import java.util.List;

import com.livery.app.model.observators.BookingObserver;
import com.livery.app.model.observators.CarObserver;
import com.livery.app.model.observators.ClientObserver;
import com.livery.app.model.observators.TransactionObserver;
import com.livery.app.model.observators.WorkerObserver;
import com.livery.data.Booking;
import com.livery.data.Car;
import com.livery.data.Client;
import com.livery.data.Transaction;
import com.livery.data.Worker;

public class WorkspaceModel implements AbstractWorkspaceModel{

	private List<Client> clientList;
	private List<Car> carList;
	private List<Booking> bookingList;
	private List<Worker> workerList;
	private List<Transaction> transactionList;
	
	private ArrayList<TransactionObserver> transactionObservers;
	private ArrayList<ClientObserver> clientObservers;
	private ArrayList<BookingObserver> bookingObservers;
	private ArrayList<CarObserver> carObservers;
	private ArrayList<WorkerObserver> workerObservers;
	
	public WorkspaceModel(){
		transactionObservers = new ArrayList<TransactionObserver>();
		clientObservers = new ArrayList<ClientObserver>();
		bookingObservers = new ArrayList<BookingObserver>();
		carObservers = new ArrayList<CarObserver>();
		workerObservers = new ArrayList<WorkerObserver>();
	}
	@Override
	public void initialize(Worker worker) {

	}

	@Override
	public List<Car> downloadCarList(int limit, int offset) {
		if(carList == null)
			carList = new ArrayList<Car>();
		// TODO pobieranie rekordow hibernatem - baza
		return carList;
		
	}

	@Override
	public List<Worker> dowloadWorkerList(int limit, int offset) {
		if(workerList == null)
			workerList = new ArrayList<Worker>();
		// TODO pobieranie rekordow hibernatem - baza
		return workerList;
	}

	@Override
	public List<Client> downloadClientList(int limit, int offset) {
		if(clientList == null)
			clientList = new ArrayList<Client>();
		// TODO pobieranie rekordow hibernatem - baza
		return clientList;
	}

	@Override
	public List<Transaction> dowloadTransactionList(int limit, int offset) {
		if(transactionList == null)
			transactionList = new ArrayList<Transaction>();
		// TODO pobieranie rekordow hibernatem - baza
		return transactionList;
	}

	@Override
	public List<Booking> downloadBookingList(int limit, int offset) {
		if(bookingList == null)
			bookingList = new ArrayList<Booking>();
		// TODO pobieranie rekordow hibernatem - baza
		return bookingList;
	}

	@Override
	public void registerObserver(CarObserver observer) {
		carObservers.add(observer);
		
	}

	@Override
	public void registerObserver(WorkerObserver observer) {
		workerObservers.add(observer);
		
	}

	@Override
	public void registerObserver(ClientObserver observer) {
		clientObservers.add(observer);
		
	}

	@Override
	public void registerObserver(TransactionObserver observer) {
		transactionObservers.add(observer);
		
	}

	@Override
	public void registerObserver(BookingObserver observer) {
		bookingObservers.add(observer);
		
	}

	@Override
	public void removeObserver(CarObserver observer) {
		int index = carObservers.indexOf(observer);
		if( index >= 0)
			carObservers.remove(index);
		
	}

	@Override
	public void removeObserver(WorkerObserver observer) {
		int index =	workerObservers.indexOf(observer);
		if( index >= 0)
			workerObservers.remove(index);
		
	}

	@Override
	public void removeObserver(ClientObserver observer) {
		int index = clientObservers.indexOf(observer);
		if( index >= 0)
			clientObservers.remove(index);
	}

	@Override
	public void removeObserver(TransactionObserver observer) {
		int index = transactionObservers.indexOf(observer);
		if( index >= 0)
			transactionObservers.remove(index);
		
	}

	@Override
	public void removeObserver(BookingObserver observer) {
		int index = bookingObservers.indexOf(observer);
		if( index >= 0)
			bookingObservers.remove(index);
		
	}

	@Override
	public void notifyCarObservers() {
		for(int i = 0; i < carObservers.size(); i++){
			CarObserver carObs = (CarObserver) carObservers.get(i);
			carObs.updateCar(carList);
		}
		
	}

	@Override
	public void notifyWorkerObservers() {
		for(int i = 0; i < workerObservers.size(); i++){
			WorkerObserver workerObs = (WorkerObserver) workerObservers.get(i);
			workerObs.updateWorker(workerList);
		}
		
	}

	@Override
	public void notifyClientObservers() {
		for(int i = 0; i < clientObservers.size(); i++){
			ClientObserver clientObs = (ClientObserver) clientObservers.get(i);
			clientObs.updateClient(clientList);
		}
		
	}

	@Override
	public void notifyTransactionObservers() {
		for(int i = 0; i < transactionObservers.size(); i++){
			TransactionObserver transactionObs = (TransactionObserver) transactionObservers.get(i);
			transactionObs.updateTransaction(transactionList);
		}
		
	}

	@Override
	public void notifyBookingObservers() {
		for(int i = 0; i < bookingObservers.size(); i++){
			BookingObserver bookingObs = (BookingObserver) bookingObservers.get(i);
			bookingObs.updateBooking(bookingList);
		}
		
	}

}
